<?php

/**
 * The ReturnPagSeguroCore class is a controller of the Design Pattern.
 * 
 * @abstract LayoutPage
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
class ReturnPagSeguroCore extends LayoutPage {

    public function preProcess() {
        parent::preProcess();
        
        $pagseguro = new PagSeguro('8A916F696A4B43BAB5681D3D2AC27275');
        if (isset($_POST) && $pagseguro->verify($_POST)) {
            $type = $_POST['ProdID_1'][0];
            
            $id = substr($_POST['ProdID_1'], 1);
            switch ($_POST['StatusTransacao']) {
                case 'Aprovado':
                    
                    if ($type == 'F') {
                        $data = array(
                            'id' => $_POST['TransacaoID'],
                            'id_customer_vehicle' => $id
                        );
                        DbSet::customers_transactions($data);
                        DbSet::customers_vehicles(array('id_status' => 3), $id);
                    }
                    else {
                        $data = array(
                            'id' => $_POST['TransacaoID'],
                            'id_enterprise' => $id
                        );
                        DbSet::enterprises_transactions($data);
                        DbSet::enterprises(array('id_status' => 3), $id);
                    }
                    exit();
                    break;
                case 'Cancelado':
                    if ($type == 'F')
                        DbSet::customers_vehicles(array('id_status' => 1), $id);
                    else
                        DbSet::enterprises(array('id_status' => 1), $id);
                    exit();
                    break;
                default:
                    exit();
                    break;
            }
        }
    }
    
    public function displayHeader() {
        global $_tag_title, $_metatag_description, $_metatag_keywords;
        
        $_tag_title = 'Pedido Efetuado com Sucesso | Goiautos.com.br';
        $_metatag_description = '.';
        $_metatag_keywords = str_word_count(mb_strtolower('carros motos novos seminovos usados caminhões veículos classificados goiás pagseguro', 'UTF-8'), 1, 'áéíóúàâêôçãõ0123456789');
        
        Shell::showPage('head');
    }
    
    public function displayContent() {
        Shell::showPage('return-pagseguro');
    }
    
    public function displayFooter() {}

}